package com.xungou.comment.service.impl;

import com.xungou.attachment.dao.IAttachment;
import com.xungou.attachment.pojo.Attachment;
import com.xungou.comment.dao.IComment;
import com.xungou.comment.pojo.Comment;
import com.xungou.comment.service.ICommentService;
import com.xungou.core.mvc.AbstractService;
import com.xungou.core.mvc.IDao;
import com.xungou.good.dao.IGood;
import com.xungou.good.pojo.Good;
import com.xungou.score.dao.IScore;
import com.xungou.score.pojo.Score;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("commentService")
public class CommentServiceImpl extends AbstractService<Comment> implements ICommentService {
    @Resource
    private IComment iComment;
    @Resource
    private IScore iScore;
    @Resource
    private IAttachment iAttachment;

    @Override
    protected IDao<Comment> getDao() {
        return iComment;
    }

    @Override
    public Map<String, Object> add(Comment comment, Score score, Map<String, String> params) {
        comment.setTime(new Date());
        int flag = iComment.insert(comment);
        iScore.insert(score);
        Attachment attachment;
        String[] photoIds = params.get("photoIds").split(",");
        for (String photoId : photoIds) {
            attachment = new Attachment();
            attachment.setBusinessCode("comment");
            attachment.setBusinessId(comment.getId());
            attachment.setResource(Integer.parseInt(photoId));
            iAttachment.insert(attachment);
        }
        Map<String, Object> result = new HashMap<>();
        result.put("flag",flag);
        return result;
    }

    @Override
    public List<Map<String, Object>> list(Map<String, String> params) {
        return iComment.list(params);
    }
}